using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PoolManager : MonoBehaviour
{
	public List<PoolManagerEntry> objSettings = new List<PoolManagerEntry>();

	public static PoolManager instance;

	[HideInInspector]
	public List<GameObject> allPoolObjects;

	private Dictionary<string, List<PoolManagerRec>> _Pool = new Dictionary<string, List<PoolManagerRec>>();

	private void Awake()
	{
		instance = this;
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private void Start()
	{
		if (_Pool.Count == 0)
		{
			InitBaseOnMod();
		}
	}

	public void InitBaseOnMod()
	{
		if (_Pool != null && _Pool.Count > 0)
		{
			DestroyMobPool();
		}
		_Pool = new Dictionary<string, List<PoolManagerRec>>();
		FillingThePool(ref _Pool, ref objSettings);
	}

	public void DestroyMobPool()
	{
		foreach (KeyValuePair<string, List<PoolManagerRec>> item in _Pool)
		{
			for (int i = 0; i < item.Value.Count; i++)
			{
				Object.Destroy(item.Value[i].go);
			}
		}
		_Pool.Clear();
		foreach (GameObject allPoolObject in allPoolObjects)
		{
			Object.Destroy(allPoolObject);
		}
		allPoolObjects.Clear();
	}

	public GameObject TakeFromPool(string poolName, Transform newParent)
	{
		GameObject gameObject = null;
		foreach (PoolManagerRec item in _Pool[poolName])
		{
			if (item.isFree)
			{
				item.isFree = false;
				item.go.transform.parent = newParent;
				gameObject = item.go;
				break;
			}
		}
		if (gameObject == null)
		{
			gameObject = AddOneObjectToPool(poolName);
		}
		return gameObject;
	}

	public void ReturnToPool(string poolName, GameObject go)
	{
		foreach (PoolManagerRec item in _Pool[poolName])
		{
			if (item.go == go)
			{
				item.isFree = true;
				item.go.transform.parent = base.transform;
				go.SetActive(false);
				go.transform.position = Vector3.down * 1000f;
				break;
			}
		}
	}

	private IEnumerator Init()
	{
		foreach (PoolManagerEntry entry in objSettings)
		{
			if (entry.textureChanger)
			{
				GameObject newGO2 = new GameObject();
				newGO2 = ((!(entry.weakPrefab != null) || Device.indexWeakDevice == 0) ? Object.Instantiate(entry.strongPrefab) : Object.Instantiate(entry.weakPrefab));
				yield return null;
				TextureChanger[] arrayText = newGO2.GetComponentsInChildren<TextureChanger>(true);
				TextureChanger[] array = arrayText;
				foreach (TextureChanger element in array)
				{
				}
				newGO2.transform.parent = base.transform;
				newGO2.SetActive(false);
				if (entry.weakPrefab != null && Device.indexWeakDevice != 0)
				{
					entry.weakPrefab = newGO2;
				}
				else
				{
					entry.strongPrefab = newGO2;
				}
			}
		}
	}

	private void FillingThePool(ref Dictionary<string, List<PoolManagerRec>> tempPool, ref List<PoolManagerEntry> tempSettings)
	{
		foreach (PoolManagerEntry tempSetting in tempSettings)
		{
			List<PoolManagerRec> list = new List<PoolManagerRec>();
			if (tempSetting.textureChanger)
			{
				tempSetting.count = 110;
			}
			if ((!tempSetting.isMob || settings.offlineMode) && (!Device.isWeakDevice || !tempSetting.forHardDevice))
			{
				for (int i = 0; i < tempSetting.count; i++)
				{
					GameObject gameObject = ((!(tempSetting.weakPrefab != null) || Device.indexWeakDevice == 0) ? Object.Instantiate(tempSetting.strongPrefab) : Object.Instantiate(tempSetting.weakPrefab));
					allPoolObjects.Add(gameObject);
					gameObject.transform.parent = base.transform;
					gameObject.transform.localPosition = Vector3.zero;
					PoolManagerRec poolManagerRec = new PoolManagerRec();
					poolManagerRec.go = gameObject;
					poolManagerRec.isFree = true;
					gameObject.SetActive(false);
					gameObject.name = tempSetting.poolName;
					list.Add(poolManagerRec);
				}
				tempPool[tempSetting.poolName] = list;
			}
		}
	}

	private GameObject AddOneObjectToPool(string poolName)
	{
		List<PoolManagerRec> list = _Pool[poolName];
		GameObject gameObject = null;
		foreach (PoolManagerEntry objSetting in objSettings)
		{
			if (objSetting.strongPrefab != null && objSetting.poolName == poolName)
			{
				gameObject = ((!(objSetting.weakPrefab != null) || Device.indexWeakDevice == 0) ? Object.Instantiate(objSetting.strongPrefab) : Object.Instantiate(objSetting.weakPrefab));
				allPoolObjects.Add(gameObject);
				gameObject.transform.parent = base.transform;
				gameObject.transform.localPosition = Vector3.zero;
				PoolManagerRec poolManagerRec = new PoolManagerRec();
				poolManagerRec.go = gameObject;
				poolManagerRec.isFree = true;
				gameObject.SetActive(false);
				gameObject.name = objSetting.poolName;
				list.Add(poolManagerRec);
				break;
			}
		}
		_Pool[poolName] = list;
		return gameObject;
	}
}
